package com.yoomiito.app.ui.my.songdudouo

import android.view.View
import cn.droidlover.xdroidmvp.net.ApiException
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.BaseModel
import cn.droidlover.xdroidmvp.net.XApi
import com.yoomiito.app.Constant
import com.yoomiito.app.base.AppConfig
import com.yoomiito.app.base.BaseLazyFragmentPresenter
import com.yoomiito.app.model.BaseList
import com.yoomiito.app.model.my.GiveMoreIncomeDetail
import com.yoomiito.app.net.YmApi
import com.yoomiito.app.showToast

class GiveMoreIncomeFragmentPresenter(appConfig: AppConfig) : BaseLazyFragmentPresenter<GiveMoreIncomeFragment>(appConfig){

    //refreshPartData:Boolean = false 备用：是否局部刷新的标记
    fun getData(page:Int,type:Int){
        YmApi.getYmService()
                .giveMoreIncome(type,page, Constant.PAGE_SIZE)
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object : ApiSubscriber<BaseList<GiveMoreIncomeDetail>>() {

                    override fun onFail(error: ApiException) {
                        v?.dismissLoading()
                        error?.msg?.showToast()
                    }

                    override fun onSuccess(t: BaseList<GiveMoreIncomeDetail>?) {
//                        if (refreshPartData){

//                        }else{
                            v?.setData(t)
//                        }
                    }
                })
    }

    fun getTip(view: View, orderId:String) {
        YmApi.getYmService()
                .getFreeGiftOrderTip(orderId)
                .compose(v?.bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object :ApiSubscriber<List<String>>(){
                    override fun onSuccess(t: List<String>?) {
                        v?.setTip(view,t)
                    }

                    override fun onFail(error: ApiException?) {
                        v?.dismissLoading()
                        error?.msg?.showToast()
                    }

                })
    }

    fun submitSettlement(orderId:String){
        YmApi.getYmService()
                .submitSettlement(orderId)
                .compose(v?.bindToLifecycle())
                .compose(XApi.getApiTransformerNoData<BaseModel<*>>())
                .compose(XApi.getScheduler())
                .subscribe(object :ApiSubscriber<BaseModel<*>>(){
                    override fun onSuccess(t: BaseModel<*>?) {
                        v?.settleResult(true)
                    }

                    override fun onFail(error: ApiException?) {
                        v?.settleResult(false)
                        "申请失败,请稍后再试".showToast()
                    }
                })
    }
}